define(['./BaseView', '../models/categoryCollection', '../libs/extend', 'underscore'], function (BaseView, CategoryCollection, extend, _) {
    return BaseView.extend({
        fieldCollection: null,
        num: null,
        events: {
            "click .list-item": "cellClickToDetailPage",
            "click #theMore": "theMore",
            "click .toSearch": "toSearch",
            "click #myDataList": "myDataList",
            "click #addData": "addData",
            "click #statistics": "statistics"
        },
        //初始化
        initialize: function () {
            num = 2;
            this.fieldCollection = new CategoryCollection();
            //监听reset
            this.listenTo(this.fieldCollection, 'reset', this.onRest);
            var template = library.getTemplate('search.html');
            this.$el.append(template);
        },
        //渲染基础模版
        render: function () {
            var wrapperDom = [
                '<div id="scroller-content" class="bottom-list-area">',
                '<ul class="dataList"></ul>',
                '</div>',
            ].join(''), $wrapperDom = $(wrapperDom);
            this.$el.append($wrapperDom);
            return this;
        },
        //数据reset后重新渲染页面
        onRest: function () {
            var data = this.fieldCollection.toJSON();
            var that = this;
            this.createList(data);
            //监听滚动条。延迟加载
            var switchs = false;
            $('#scroller-content').bind({
                touchstart:function(e){
                    var touch = e.originalEvent.targetTouches[0];//获取第一个触点
                    var y = Number(touch.pageY); //页面触点Y坐标
                    //记录触点初始位置
                    startY = y;
                },
                touchmove: function (e) {
                    // e.preventDefault();
                    var touch = e.originalEvent.targetTouches[0]; //获取第一个触点
                    var y = Number(touch.pageY); //页面触点Y坐标
                    var scrolltop = $(this).scrollTop();
                    if(scrolltop<=0){
                        if (y - startY >= 30) {
                            $('.dataList').css('transform','translateY(30px)');
                        } else if(y - startY>=0 && y - startY<30){
                            var lengthY =  y - startY ;
                            $('.dataList').css('transform','translateY('+lengthY+'px)');
                            $('.pulldown').css('transform','translateY('+lengthY+'px)');
                            switchs = true;
                        }
                    }
                }
            });
            $('#scroller-content').bind('touchcancel touchend', function () {
                var height = $(this).height();
                var scrolltop = $(this).scrollTop();
                var offsetH = $('.dataList').outerHeight();
                $('.pulldown').css('transform','translateY(0px)');
                that.scroll();
                if (scrolltop >= offsetH - height - 50 && scrolltop != 0) {
                    $('#scroller-content').unbind('scroll');
                    that.$el.find('.pullup').hide();
                    that.$el.find('.loading').show();
                    indexData = {
                        "pageindex": num,
                        "pagesize": 10
                    };
                    extend.sendData(window.app.proxy.root + 'allResearchRecords', indexData, function (res) {
                        if (res.status == 'success') {
                            if (res.data.length == 0) {
                                that.$el.find('.loading').hide();
                                that.$el.find('.noMore').show();
                                that.$el.find('.pullup').hide();
                                setTimeout(
                                    function () {
                                        that.$el.find('.noMore').hide();
                                    }, 300
                                );
                                return false
                            } else {
                                setTimeout(function () {
                                    that.createList(res.data);
                                    num++;
                                }, 300)
                            }
                        } else {
                            extend.Toast(res.message, 2000);
                        }
                    }, function () {
                        extend.Toast('网络错误', 2000);
                    })
                }
                if(switchs == true){
                    var indexData = {
                        "pageindex": 1,
                        "pagesize": 10
                    };
                    extend.sendData(window.app.proxy.root + 'allResearchRecords', indexData, function (res) {
                        if (res.status == 'success') {
                            if (res.data.length !== 0) {
                                that.$el.find('.loadingTop').show();
                                setTimeout(
                                    function () {
                                        $('.dataList').css('transform','translateY(0px)').html('');
                                        that.$el.find('.loadingTop').hide();
                                        that.createList(res.data);
                                    }, 300
                                );
                                return false
                            }
                        } else {
                            extend.Toast(res.message, 2000);
                        }
                    }, function (e) {
                        extend.Toast('网络错误', 2000);
                    });
                    switchs = false;
                }
            });

        },
        scroll: function () {
            $('#scroller-content').bind("scroll", function () {
                var height = $(this).height();
                var scrolltop = $(this).scrollTop();
                var offsetH = $('.dataList').outerHeight();
                if (scrolltop >= offsetH - height) {
                    $('.pullup').show();
                } else {
                    $('.pullup').hide();
                }
            });
        },
        //根据数据渲染主题列表
        createList: function (data) {
            var len = data.length;
            if (len) {
                for (var i = 0; i < len; i++) {
                    var item = data[i];
                    var itemId = item.pk_record;
                    var Username = item.username; //人員姓名
                    var project = item.project; //項目
                    var begindate = item.begindate;//起始時間
                    if (item) {
                        var listItemDom = [
                                '<li class="list-item" data-id=' + itemId + '>',
                                '<p class="project">' + project + '</p>',
                                '<p class="username"><span>负责人：</span><span>' + Username + '</span></p>',
                                '<span class="begindate">' + begindate + '</span>',
                                '</li>'
                            ].join(''),
                            $listItemDom = $(listItemDom);
                        $('.dataList').append($listItemDom);
                    }
                }
                this.$el.find('.loading').hide();
            } else {
                extend.Toast('没有数据', 2000);
            }
        },
        //进入详情页
        cellClickToDetailPage: function (e) {
            var dataid = e.currentTarget.getAttribute("data-id");
            //存在本地数据库
            extend.setlocalStorage('YY_themsID', dataid);
            Backbone.history.navigate('#Particular', {
                trigger: true
            });
        },
        //下拉菜单
        theMore: function () {
            if ($('#theMore').hasClass('toDown')) {
                $('#theMore').removeClass('toDown').addClass('toUp');
                $('.moreList').show();
            } else {
                $('#theMore').removeClass('toUp').addClass('toDown');
                $('.moreList').hide();
            }
            return false;
        },
        //跳转我的调研记录页面
        myDataList: function () {
            $('.moreList').hide();
            $('#theMore').removeClass('toUp').addClass('toDown');
            Backbone.history.navigate('#OwnParticular', {trigger: true});
        },
        //跳转到新增记录页面
        addData: function () {
            $('.moreList').hide();
            $('#theMore').removeClass('toUp').addClass('toDown');
            Backbone.history.navigate('#addData', {trigger: true});
        },
        //进入搜索页面
        toSearch: function () {
            Backbone.history.navigate('#searchPage', {
                trigger: true
            });
        },
        statistics: function () {
            Backbone.history.navigate('#statisticsPage', {
                trigger: true
            })
        }
    });
});
